import time

from Thread_example import spider
import threading


# 单线程
def single_thread():
    for url in spider.urls:
        print(url)
        spider.craw(url)


# 多线程
def multi_thread():
    threads = []    # 线程列表
    for url in spider.urls:
        print(url)
        threads.append( # 添加线程
            threading.Thread(target=spider.craw, args=(url,))   # 参数为元组类型，此处不加逗号则为字符串，加逗号使其表示为元组
        )
    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()   # 等待结束


if __name__ == "__main__":
    print("single_thread")
    start = time.time()
    single_thread()
    print("single_thread", time.time() - start)

    print("multi_thread")
    start = time.time()
    multi_thread()
    print("multi_thread", time.time() - start)
